package com.spc.car.data.service.project.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.spc.car.data.constant.CommConstants;
import com.spc.car.data.domain.project.po.SaveTenderSectionPO;
import com.spc.car.data.entity.project.ProjectTenderingSection;
import com.spc.car.data.mapper.project.ProjectTenderingSectionMapper;
import com.spc.car.data.service.project.ProjectTenderingSectionService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * <p>
 * 标段(包)表 服务实现类
 * </p>
 *
 * @author 
 * @since 2020-04-03
 */
@Service
public class ProjectTenderingSectionServiceImpl extends ServiceImpl<ProjectTenderingSectionMapper, ProjectTenderingSection> implements ProjectTenderingSectionService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int saveOrUpdateTenderSection(List<SaveTenderSectionPO> saveTenderSectionPOs) {
        ProjectTenderingSection projectTenderingSection = new ProjectTenderingSection();
        AtomicInteger row = new AtomicInteger();
        saveTenderSectionPOs.stream().forEach(anTenderSectionPO ->{
            BeanUtils.copyProperties(anTenderSectionPO, projectTenderingSection);
            if (null !=projectTenderingSection.getId() && 0 < projectTenderingSection.getId()){
                projectTenderingSection.setModifyTime(new Date());
            }else {
                projectTenderingSection.setSectionCode(generateBlockNumber());
            }
            if (saveOrUpdate(projectTenderingSection)){
                row.set(1);
            }else {
                row.set(0);
            }
        });
        return row.get();
    }

    @Override
    public String generateBlockNumber() {
        StringBuffer biddingProjectCode = new StringBuffer();
        //设置日期格式
        SimpleDateFormat dateStyle = new SimpleDateFormat("yyyyMMdd");
        String date = dateStyle.format(new Date(System.currentTimeMillis()));
        int numberOfCode = count(new QueryWrapper<ProjectTenderingSection>()
                .like("section_code","BID"+date)
        );
        if (numberOfCode > 0){
            numberOfCode++;
            String endNumber = String.format("%03d", numberOfCode);
            biddingProjectCode = new StringBuffer("BID").append(date).append(endNumber);
        }else {
            biddingProjectCode = new StringBuffer("BID").append(date).append("001");
        }
        return String.valueOf(biddingProjectCode);
    }

    @Override
    public int disableBidById(Long sectionId) {
        ProjectTenderingSection projectTenderingSection = getById(sectionId);
        projectTenderingSection.setStatus(CommConstants.TenderSections.Disabled_Bid);
        if (updateById(projectTenderingSection)){
            return 1;
        }else {
            return 0;
        }
    }

    @Override
    public ProjectTenderingSection getTenderSectionInfo(Long sectionId, String sectionCode) {
        if (ObjectUtils.isEmpty(sectionId) && StringUtils.isBlank(sectionCode)){
            return null;
        }
        return getOne(new QueryWrapper<ProjectTenderingSection>()
                .eq(ObjectUtils.isNotEmpty(sectionId), "id", sectionId)
                .eq(StringUtils.isNotBlank(sectionCode), "section_code", sectionCode)
        );
    }

    @Override
    public List<ProjectTenderingSection> listByTenderingCode(String projectTenderingCode) {
        List<ProjectTenderingSection> sectionList = new ArrayList<>();
        if (StringUtils.isBlank(projectTenderingCode)) {
            return sectionList;
        }

        return list(new QueryWrapper<ProjectTenderingSection>()
                .eq("project_tendering_code", projectTenderingCode));
    }
}
